package com.kikt.autoscrollviewpager;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.AutoScrollViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends AppCompatActivity {

    private AutoScrollViewPager vp;
    protected FragmentPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return MyFragment.newInstance(position);
            }

            @Override
            public int getCount() {
                return 5;
            }
        };
        vp.setAdapter(adapter);
    }

    public void test(View view) {
        adapter.notifyDataSetChanged();
    }

    public static class MyFragment extends Fragment {
        private int position;

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View inflate = inflater.inflate(R.layout.fragment_bg, container, false);
            int all = 0xffffff;
            String s = Integer.toHexString(all / (position + 1));
            Log.d("MyFragment", "position:" + position);
            Log.d("MyFragment", s);
            int color = Color.parseColor("#" + s);
            inflate.setBackgroundColor(color);
            return inflate;
        }

        public static MyFragment newInstance(int position) {
            MyFragment fragment = new MyFragment();
            fragment.setPosition(position);
            return fragment;
        }

        private void setPosition(int position) {
            this.position = position;
        }
    }


    private void initView() {
        vp = (AutoScrollViewPager) findViewById(R.id.vp);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }
}
